package com.naiterui.ehp.bs.consult.controller.inner;

import com.alibaba.fastjson.JSONObject;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bs.consult.constant.VideoConsultConstant;
import com.naiterui.ehp.bs.consult.service.IVideoConsultNoticeService;
import com.naiterui.ehp.bs.consult.service.IVideoConsultService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 视频问诊回调通知
 *
 * @author yangfuya
 * @version 1.0
 * @date 2021-02-28 15:50
 **/
@Slf4j
@RestController
@RequestMapping("/inner/video/consult/notice")
@RequiredArgsConstructor
@Api(tags = SwaggerTag.VIDEO + SwaggerTag.INNER_API + SwaggerTag.OTHER)
public class InnerVideoConsultNoticeController {

    private final IVideoConsultNoticeService videoConsultNoticeService;
    private final IVideoConsultService videoConsultService;

    @RequestMapping
    public void notice(@RequestBody JSONObject json) {
        int eventGroupId = json.getIntValue("EventGroupId");
        // 1为房间时间组 2.媒体事件组
        if (eventGroupId == VideoConsultConstant.EVENT_GROUP_ROOM) {
            boolean containsEventType = json.containsKey("EventType");
            if (containsEventType) {
                int eventType = json.getIntValue("EventType");
                JSONObject eventInfo = json.getJSONObject("EventInfo");
                switch (eventType) {
                    // 创建房间
                    case VideoConsultConstant.EVENT_TYPE_CREATE_ROOM:
                        log.info("创建房间：{}", json.toJSONString());
                        videoConsultNoticeService.createRoom(eventInfo);
                        break;
                    // 解散房间
                    case VideoConsultConstant.EVENT_TYPE_DISMISS_ROOM:
                        log.info("解散房间：{}", json.toJSONString());
                        videoConsultNoticeService.dismissRoom(eventInfo);
                        break;
                    // 进入房间
                    case VideoConsultConstant.EVENT_TYPE_ENTER_ROOM:
                        log.info("进入房间：{}", json.toJSONString());
                        videoConsultNoticeService.inRoom(eventInfo);
                        break;
                    // 退出房间
                    case VideoConsultConstant.EVENT_TYPE_EXIT_ROOM:
                        log.info("退出房间：{}", json.toJSONString());
                        videoConsultNoticeService.outRoom(eventInfo);
                        break;
                    default:
                        log.info("其他通知：{}", json.toJSONString());
                        break;
                }
            } else {
                log.info("其他事件组通知:{}", json.toJSONString());
            }
        }
    }

    /**
     * 上传视频问诊永久素材
     *
     * @param file
     */
    @PostMapping("/uploadThumbMedia")
    public void uploadThumbMedia(MultipartFile file) {
        videoConsultService.uploadThumbMedia(file);
    }

    /**
     * 录制回调
     *
     * @param json
     */
    @RequestMapping("/transcribe")
    public void transcribe(@RequestBody JSONObject json) {
        log.info("录制回调：" + json.toJSONString());
    }
}